package com.system.permission.web;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.permission.entity.Permission;
import com.system.permission.entity.DTO.PermissionQueryDTO;
import com.system.permission.service.IPermissionService;
@Controller
@RequestMapping("/permission")
public class PermissionController 
{
	@Autowired
	private IPermissionService permissionService;

	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Permission permission) 
	{
		if(permission.getPermissionId()==null) {
			try {
				permissionService.saveOrUpdate(permission);
				return new ExtAjaxResponse(true,"用户添加成功");
			}catch(Exception e){
				return new ExtAjaxResponse(false,"用户添加失败");}
		}else{	
			try {		
				permissionService.saveOrUpdate(permission);
				return new ExtAjaxResponse(true,"用户信息更新成功");
			}catch (Exception e) {
				return new ExtAjaxResponse(false,"用户信息更新失败");
			}	}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam Long permissionId) {
		try {
			Permission permission = permissionService.findOne(permissionId);
			permissionService.delete(permission);
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/deletePermissions")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam Long[] ids) {
		try {
			List<Long> lists = Arrays.asList(ids);
			if (null!=lists) {
				permissionService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Permission findOne(@RequestParam Long permissionId) 
	{
		Permission permission =  permissionService.findOne(permissionId);
		
		return permission;
	}
	
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Permission> findAll() 
	{
		 List<Permission> lists =  permissionService.findAll();
		
		return lists;
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<Permission> findPage(PermissionQueryDTO permissionQueryDTO,ExtPageable pageable) 
	{
		Page<Permission> page =  permissionService.findAll(PermissionQueryDTO.getSpecification(permissionQueryDTO), pageable.getPageable());
		
		return page;
	}
}
